package com.edu.exam.entity.assessment;

import com.edu.exam.enums.assessment.TaskAssignmentStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 任务分配实体类
 * 所属模块：B-05 考核计划管理模块
 * 对应数据库表：task_assignments
 *
 * 管理考核任务的分配信息，包括分配编码、学生、状态等。
 * 支持任务分配的创建、查询、更新和状态管理。
 * 关联考核任务、学生、教师等关键信息，提供任务分配的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class TaskAssignment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "分配编码不能为空")
    @Size(max = 50, message = "分配编码长度不能超过50个字符")
    @Column(name = "assignment_code", nullable = false, length = 50, unique = true)
    private String assignmentCode;

    @NotNull(message = "任务ID不能为空")
    @Column(name = "task_id", nullable = false)
    private Long taskId;

    @Column(name = "task_code", length = 50)
    private String taskCode;

    @Column(name = "task_name", length = 200)
    private String taskName;

    @NotNull(message = "学生ID不能为空")
    @Column(name = "student_id", nullable = false)
    private Long studentId;

    @Column(name = "student_code", length = 20)
    private String studentCode;

    @Column(name = "student_name", length = 100)
    private String studentName;

    @Column(name = "student_class", length = 50)
    private String studentClass;

    @NotNull(message = "分配状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "assignment_status", nullable = false, length = 20)
    private TaskAssignmentStatus assignmentStatus;

    @Column(name = "assigned_by", length = 50)
    private String assignedBy;

    @CreatedDate
    @Column(name = "assigned_time", nullable = false, updatable = false)
    private LocalDateTime assignedTime;

    @Column(name = "assignment_deadline")
    private LocalDateTime assignmentDeadline;

    @Column(name = "start_time")
    private LocalDateTime startTime;

    @Column(name = "end_time")
    private LocalDateTime endTime;

    @Column(name = "time_taken_seconds")
    private Integer timeTakenSeconds; // 实际用时（秒）

    @Column(name = "attempt_number")
    @Builder.Default
    private Integer attemptNumber = 1;

    @Column(name = "max_attempts")
    private Integer maxAttempts;

    @Column(name = "remaining_attempts")
    private Integer remainingAttempts;

    @Column(name = "ip_address", length = 50)
    private String ipAddress;

    @Column(name = "user_agent", length = 500)
    private String userAgent;

    @Column(name = "browser_info", length = 200)
    private String browserInfo;

    @Column(name = "device_info", length = 200)
    private String deviceInfo;

    @Column(name = "location_info", length = 200)
    private String locationInfo;

    @Column(name = "access_method", length = 20)
    private String accessMethod;

    @Column(name = "session_id", length = 100)
    private String sessionId;

    @Column(name = "submitted_answers", columnDefinition = "TEXT")
    private String submittedAnswers;

    @Column(name = "answer_data", columnDefinition = "TEXT")
    private String answerData;

    @Column(name = "auto_score")
    private BigDecimal autoScore;

    @Column(name = "manual_score")
    private BigDecimal manualScore;

    @Column(name = "total_score")
    private BigDecimal totalScore;

    @Column(name = "max_score")
    private BigDecimal maxScore;

    @Column(name = "percentage")
    private BigDecimal percentage;

    @Column(name = "passed")
    private Boolean passed;

    @Column(name = "grade_level", length = 20)
    private String gradeLevel;

    @Column(name = "feedback", columnDefinition = "TEXT")
    private String feedback;

    @Column(name = "grader_id")
    private Long graderId;

    @Column(name = "grader_name", length = 50)
    private String graderName;

    @Column(name = "graded_time")
    private LocalDateTime gradedTime;

    @Column(name = "grading_notes", length = 1000)
    private String gradingNotes;

    @Column(name = "review_required")
    @Builder.Default
    private Boolean reviewRequired = false;

    @Column(name = "reviewed_by", length = 50)
    private String reviewedBy;

    @Column(name = "review_time")
    private LocalDateTime reviewTime;

    @Column(name = "review_comments", length = 1000)
    private String reviewComments;

    @Column(name = "suspicion_level")
    private Integer suspicionLevel; // 异常行为嫌疑等级 0-5

    @Column(name = "suspicion_details", length = 1000)
    private String suspicionDetails;

    @Column(name = "violation_detected")
    @Builder.Default
    private Boolean violationDetected = false;

    @Column(name = "violation_details", length = 1000)
    private String violationDetails;

    @Column(name = "penalty_applied")
    @Builder.Default
    private Boolean penaltyApplied = false;

    @Column(name = "penalty_details", length = 1000)
    private String penaltyDetails;

    @Column(name = "extension_requested")
    @Builder.Default
    private Boolean extensionRequested = false;

    @Column(name = "extension_reason", length = 1000)
    private String extensionReason;

    @Column(name = "extension_approved")
    @Builder.Default
    private Boolean extensionApproved = false;

    @Column(name = "extension_time")
    private LocalDateTime extensionTime;

    @Column(name = "new_deadline")
    private LocalDateTime newDeadline;

    @Column(name = "special_arrangements", length = 1000)
    private String specialArrangements;

    @Column(name = "accommodations", length = 1000)
    private String accommodations;

    @Column(name = "completion_rate")
    private BigDecimal completionRate;

    @Column(name = "question_answered")
    @Builder.Default
    private Integer questionAnswered = 0;

    @Column(name = "question_skipped")
    @Builder.Default
    private Integer questionSkipped = 0;

    @Column(name = "question_correct")
    @Builder.Default
    private Integer questionCorrect = 0;

    @Column(name = "question_incorrect")
    @Builder.Default
    private Integer questionIncorrect = 0;

    @Column(name = "last_activity_time")
    private LocalDateTime lastActivityTime;

    @Column(name = "idle_time_seconds")
    private Integer idleTimeSeconds;

    @Column(name = "tab_switches")
    @Builder.Default
    private Integer tabSwitches = 0;

    @Column(name = "copy_attempts")
    @Builder.Default
    private Integer copyAttempts = 0;

    @Column(name = "paste_attempts")
    @Builder.Default
    private Integer pasteAttempts = 0;

    @Column(name = "right_click_attempts")
    @Builder.Default
    private Integer rightClickAttempts = 0;

    @Column(name = "fullscreen_exits")
    @Builder.Default
    private Integer fullscreenExits = 0;

    @Column(name = "camera_detected")
    @Builder.Default
    private Boolean cameraDetected = false;

    @Column(name = "microphone_detected")
    @Builder.Default
    private Boolean microphoneDetected = false;

    @Column(name = "other_devices_detected")
    @Builder.Default
    private Boolean otherDevicesDetected = false;

    @Column(name = "network_interruptions")
    @Builder.Default
    private Integer networkInterruptions = 0;

    @Column(name = "session_logs", columnDefinition = "TEXT")
    private String sessionLogs;

    @Column(name = "screenshot_urls", length = 1000)
    private String screenshotUrls;

    @Column(name = "video_recording_url", length = 500)
    private String videoRecordingUrl;

    @Column(name = "notes", length = 1000)
    private String notes;

    @Column(name = "internal_comments", columnDefinition = "TEXT")
    private String internalComments;

    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", insertable = false, updatable = false)
    private AssessmentPlanTask assessmentTask;

}